
package org.argeproje.resim.ui.model.commands;

import org.argeproje.resim.ui.model.Shape;
import org.argeproje.resim.ui.model.ShapesDiagram;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.requests.CreateRequest;


/**
 * A command to add a Shape to a ShapeDiagram. The command can be undone or
 * redone.
 * 
 */
public class ShapeCreateCommand extends Command {
	/** The new shape. */
	private Shape newShape;

	/** ShapeDiagram to add to. */
	private final ShapesDiagram parent;
	/** A request to create a new Shape. */
	private final CreateRequest request;
	/** True, if newShape was added to parent. */
	private boolean shapeAdded;

	/**
	 * Create a command that will add a new Shape to a ShapesDiagram.
	 * 
	 * @param parent
	 *            the ShapesDiagram that will hold the new element
	 * @param req
	 *            a request to create a new Shape
	 * @throws IllegalArgumentException
	 *             if any parameter is null, or the request does not provide a
	 *             new Shape instance
	 */
	public ShapeCreateCommand(ShapesDiagram parent, CreateRequest req) {
		if ( (parent == null) || 
			 (req == null)    ||
			(Shape.class.cast(req.getNewObject()))==null) {
			throw new IllegalArgumentException();
			
		}
		this.parent = parent;
		this.request = req;
		setLabel("shape creation");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#canUndo()
	 */
	public boolean canUndo() {
		return shapeAdded;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#execute()
	 */
	public void execute() {
		// Obtain the new Shape instance from the request.
		// This causes the factory stored in the request to create a new
		// instance.
		// The factory is supplied in the palette-tool-entry, see
		// ShapesEditorPaletteFactory#createComponentsGroup()
		newShape = (Shape) request.getNewObject();
		// Get desired location and size from the request

		// no need to set size
		// newShape.setSize(request.getSize()); // might be null!

		newShape.setLocation(request.getLocation());
		redo();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#redo()
	 */
	public void redo() {
		shapeAdded = parent.addChild(newShape);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#undo()
	 */
	public void undo() {
		parent.removeChild(newShape);
	}

}
